<?xml version="1.0" encoding="UTF-8" ?>
<%@ page
	contentType="text/html;charset=UTF-8"
	pageEncoding="UTF-8"
	import="java.util.List"
	import="java.util.HashMap"
	import="fr.utbm.gi.in56.vehicle.BrandEntry"
	import="fr.utbm.gi.in56.vehicle.ModelEntry"
	import="fr.utbm.gi.in56.error.ErrorType"
	import="fr.utbm.gi.in56.ads.AdSell"
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <link rel="stylesheet" media="screen" type="text/css" title="Style" href="style.css" />
        <script type="text/javascript" src="ressources/scripts/modelList.js"></script>
        <script type="text/javascript" src="ressources/scripts/dynamicInput.js"></script> 
        <title>L'Entre-Pot</title>
    </head>
    <body>
        <div id ="container">
            <jsp:directive.include file="header.jsp"/>
            <jsp:directive.include file="topMenu.jsp"/>
            <div id="core">
            	<h2 align="center">Rechercher une annonce de vente</h2>
            	<div id="searchAd">
            		<% ErrorType error = (ErrorType)request.getAttribute( "searchAdError" );%>
					<%
						if( error != null )
						{
					%>
							<div id="error">
							    <%= error.getErrorMessage() %>
							</div>
					<%
						}
					%>
            		<form method="post" action="SearchSellAdServlet" name="publish">
						<%
						List< BrandEntry > brandList =
							( List< BrandEntry > )request.getAttribute("brandList" );
						List< List< ModelEntry > > modelLists =
							( List< List< ModelEntry > > )request.getAttribute("modelLists" );
	
						if( ( brandList != null ) && ( modelLists != null ) )
						{
							int brandNb = brandList.size();
						%>
							<script>
							var modelIds = new Array(<% out.print( brandNb ); %> );
							var modelNames = new Array(<% out.print( brandNb ); %> );
							<%				
								for ( int i = 0; i < brandNb; ++i )
								{
									List< ModelEntry > brandModels = modelLists.get( i );
									int modelsNb = brandModels.size();
									
									out.print( "modelIds[" + i + "] = new Array(" + modelsNb + ");" );
									out.print( "modelNames[" + i + "] = new Array(" + modelsNb + ");" );
									
									for ( int j = 0; j < modelsNb; ++j )
									{
										out.print( "modelIds[" + i + "][" + j + "] = '" + brandModels.get( j ).getId() + "';" );
										
										if ( brandModels.get( j ).getModel().compareTo( "NOT_SPECIFIED" ) == 0 )
										{
											out.print( "modelNames[" + i + "][" + j + "] = 'Choisissez';" );
										}
										else
										{
											out.print( "modelNames[" + i + "][" + j + "] = '" + brandModels.get( j ).getModel() + "';" );
										}
									}
								}
							%>
							</script>
						<%
						}
						
						int idBrand 		= ( request.getParameter( "brand" ) == null ) ? 0 : 
							Integer.parseInt( request.getParameter( "brand" ) );
						int idModel 		= ( request.getParameter( "model" ) == null ) ? 0 : 
							Integer.parseInt( request.getParameter( "model" ) );
						int vehicleYearMin 	= ( request.getParameter( "vehicle_year_min" ) == null ) ? 0 : 
							Integer.parseInt( request.getParameter( "vehicle_year_min" ) );
						int vehicleYearMax 	= ( request.getParameter( "vehicle_year_max" ) == null ) ? 0 : 
							Integer.parseInt( request.getParameter( "vehicle_year_max" ) );
						//int kmNbMin 	= ( request.getParameter( "km_nb_min" ) == null ) ? -1 : 
						//	Integer.parseInt( request.getParameter( "km_nb_min" ) );
						//int kmNbMax 	= ( request.getParameter( "km_nb_max" ) == null ) ? -1 : 
						//	Integer.parseInt( request.getParameter( "km_nb_max" ) );
						int idEnergy 		= ( request.getParameter( "energy" ) == null ) ? 0 : 
							Integer.parseInt( request.getParameter( "energy" ) );
						int automatic 		= ( request.getParameter( "is_automatic" ) == null ) ? 2 : 
							Integer.parseInt( request.getParameter( "is_automatic" ) );
						%>
						
						<div id="searchAdLine">
							<select name="brand" id="brand" onchange="showCarList('brand', 'model', 'Modèle');">
								<option value="0">Marque</option>
								<%
									if ( brandList != null )
									{
										for ( BrandEntry brand : brandList )
										{
											if ( brand.getId() == idBrand )
											{												
								%>
											<option value="<%= brand.getId() %>" selected="selected"><%= brand.getBrand() %></option>
								<%
											}
											else
											{
								%>
											<option value="<%= brand.getId() %>"><%= brand.getBrand() %></option>
								<%
											}
										}
									}
								%>
							</select>

							<select name="model" id="model" disabled="disabled">
								<script language="JavaScript" type="text/JavaScript"> 
									showCarList('brand', 'model', 'Modèle');
									selectOption( 'model', '<%= idBrand %>' );
								</script>
							</select>

							<select name="vehicle_year_min" id="vehicle_year_min">
								<option value="0" selected="selected">Année min</option>
								<%
									for ( int year = 2011; year >=1945 ; --year )
									{
										if ( year == vehicleYearMin )
										{
								%>
									<option value="<%= year %>" selected="selected"><%= year %></option>
								<%
										}
										else
										{
								%>
									<option value="<%= year %>"><%= year %></option>
								<%
										}
									}
								%>
							</select>
							<select name="vehicle_year_max" id="vehicle_year_max">
								<option value="0" selected="selected">Année max</option>
								<%
									for ( int year = 2011; year >=1945 ; --year )
									{
										if ( year == vehicleYearMax )
										{
								%>
									<option value="<%= year %>" selected="selected"><%= year %></option>
								<%
										}
										else
										{
								%>
									<option value="<%= year %>"><%= year %></option>
								<%
										}
									}
								%>
							</select>
							<input type="text" name="km_nb_min" id="km_nb_min" value="Kilométrage min" class="cleardefault" style="width:100px"/> km
							<input type="text" name="km_nb_max" id="km_nb_max" value="Kilométrage max" class="cleardefault" style="width:100px"/> km

						<%
						HashMap< Integer, String > energyMap =
							(HashMap< Integer, String >)request.getAttribute("energiesList" );
						%>
							<select name="energy" id="energy">
								<option value="0" selected="selected">Energie</option>
								<%
									if ( energyMap != null )
									{
										for ( Integer energyId : energyMap.keySet() )
										{
											if ( energyId == idEnergy )
											{
								%>
										<option value="<%= energyId %>" selected="selected"><%= energyMap.get(energyId) %></option>
								<%				
											}
											else
											{
								%>
										<option value="<%= energyId %>"><%= energyMap.get(energyId) %></option>
								<%
											}

										}
									}
								%>
							</select>

							<select name="is_automatic" id="is_automatic">
								<option value="2">Boite de vitesse</option>
								<%
									if ( automatic == 0 )
									{
								%>
									<option value="0" selected="selected">Manuelle</option>
									<option value="1">Automatique</option>
								<%
									}
									else if ( automatic == 1 )
									{
								%>
									<option value="0">Manuelle</option>
									<option value="1" selected="selected">Automatique</option>
								<%
									}
									else
									{
								%>
									<option value="0">Manuelle</option>
									<option value="1">Automatique</option>
								<%		
									}
								%>
							</select>

							<input type="text" name="price_min" id="price_min" value="Prix min" class="cleardefault" style="width:60px" />&nbsp;€
							<input type="text" name="price_max" id="price_max" value="Prix max" class="cleardefault" style="width:60px" />&nbsp;€
						</div>
						<div id="searchAdButton">
							<input type="submit" value="Rechercher" name="search" onclick="clearUnchangedText()"/>
						</div>
					</form>
				</div>
				<%
					List< AdSell > adSellList = (List< AdSell >)request.getAttribute( "adSellList" );
					if ( adSellList != null )
					{
						if ( !adSellList.isEmpty() )
						{
				%>
					<div id="searchAd">
				<%
							for( AdSell currentAd : adSellList )
							{
				%>
						<div id="resultEntry">
							<div id="resultEntryTitle">
								<%= currentAd.getTitle() %>
							</div>
							<div id="resultEntryCore">
								<div id="resultEntryLeft">
				<%
								if ( !currentAd.getBrand().isEmpty() )
								{
				%>
								
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Marque
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getBrand() %>
										</div>
									</div>
				<%
								}
								
								if ( !currentAd.getModel().isEmpty() )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle"
										>Modèle
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getModel() %>
										</div>
									</div>
				<%
								}
								
								if ( currentAd.getVehicle_date() != 0 )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Année
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getVehicle_date()%>
										</div>
									</div>
				<%
								}
								
								if ( currentAd.getKmNb() != -1 )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Kilométrage
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getKmNb() %> km
										</div>
									</div>
				<%
								}
								
								if ( !currentAd.getEnergy().isEmpty() )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Énergie
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getEnergy() %>
										</div>
									</div>
				<%
								}
								
								int isAutomatic = currentAd.isAutomatic();
								if ( isAutomatic != 2 && isAutomatic != -1 )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Boîte de vitesse
										</div>
										<div id="resultEntryInfoValue">
				<%
									if ( isAutomatic == 1 )
									{
				%>
										Automatique
				<%
									}
									else
									{
				%>
										Manuelle
				<%
									}
				%>
										</div>
									</div>
				<%
								}
								
								if ( currentAd.getPrice() != -1 )
								{
				%>
									<div id="resultEntryInfo">
										<div id="resultEntryInfoTitle">
										Prix
										</div>
										<div id="resultEntryInfoValue">
										<%= currentAd.getPrice() %> €
										</div>
									</div>
				<%
								}
				%>
								</div>
								<div id="resultEntryCenter">
									<%= currentAd.getDescription() %>
								</div>
								<div id="resultEntryRight">
									<%
										if ( currentAd.isHasImage() )
										{
									%>
									<img border="0" src="ressources/images/<%= currentAd.getId() %>.jpg" width="150" height="150"/>
									<%
										}
										else
										{
									%>
									<img border="0" src="ressources/images/no_image.jpg" width="150" height="150"/>
									<%
										}
									%>
								</div>
							</div>
							<div id="resultEntryFooter">
				<%
					if ( currentUser != null )
					{
				%>
								<a href="SendMessageServlet?recipient=<%= currentAd.getLoginSeller() %>&adId=<%= currentAd.getId() %>&adType=sell&msgType=new
								">Contacter le vendeur</a>
				<%
					}
					else
					{
				%>
								<a href="connection.jsp">Connectez-vous pour contacter ce vendeur</a>
				<%
					}
				%>
							</div>
						</div>
				<%	
							}
				%>
					</div>
				<%
						}
					}
				%>
			</div>
            
            <jsp:directive.include file="footer.jsp"/>
        </div>
    </body>
</html>